
        package com.example.administrator.ci_edit;

        import java.io.IOException;
        import java.util.ArrayList;

        import android.app.Fragment;
        import android.app.FragmentTransaction;
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup.LayoutParams;
        import android.widget.ImageView;
        import android.widget.ImageView.ScaleType;
        import android.widget.LinearLayout;
        import android.widget.Toast;

        import activity.BaseActivity;

        import com.example.administrator.ci_edit.R;

        import db.WritingDatabaseHelper;
        import entity.Cipai;
        import entity.Writing;
        import fragment.ChangeBackgroundFragment;
        import fragment.ChangePictureFragment;
        import fragment.EditFragment;
        import util.DisplayUtil;
        import util.FileUtils;
        import util.StringUtils;
//import wiget.GCDialog;
//import wiget.GCDialog.OnCustomDialogListener;
        import wiget.TouchEffectImageView;
        import db.CipaiDatabaseHelper;

        import static db.CipaiDatabaseHelper.getPingzeById;

public class EditActivity extends BaseActivity {

    private Cipai cipai = new Cipai();

    private Writing writing;

    ChangeBackgroundFragment changeBackgroundFrament;

    EditFragment editFragment;

    ChangePictureFragment changePictureFragment;

    ArrayList<Fragment> fragments = new ArrayList<Fragment>();

    private int currentIndex = 0;

    private String oldText;

    String pingze;

    int ci_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v("tag", "edit mmp");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        Intent intent = getIntent();
        ci_id = intent.getIntExtra("extra_id",50);


        cipai.setId(ci_id);
        String pingze = getPingzeById(cipai.id);


        //writing = (Writing) getIntent().getSerializableExtra("writing");

        //cipai.setId(1);
        // 填新词
        if (cipai != null) {
            Log.v("tag", "xin ci");
            writing = new Writing();
            writing.setId(writing.hashCode());
            writing.setCi_id(cipai.getId());
            writing.setBgimg("0");
            writing.setCipai(cipai);
            /**
             * 随便写的，todo 去修改
             */

            //cipai.setPingze("<div class=\"ci\"><blockquote><div><span class=\"lingge\">平</span><span class=\"zhong\">中</span>平仄仄仄平平，<span class=\"zhong\">平</span><span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"lingge\">仄</span>平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"zhong\">中</span>仄平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"zhong\">中</span>仄平平仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><br class=\"empty\" /><div><span class=\"zhong\">中</span>仄<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，仄<span class=\"zhong\">中</span>平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div><span class=\"lingge\">仄</span>平平<span class=\"zhong\">中</span>仄，<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div>仄平平、<span class=\"zhong\">中</span>平平仄，仄<span class=\"zhong\">中</span>平、<span class=\"zhong\">中</span>仄仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div><div>平平仄、仄平平仄，<span class=\"zhong\">中</span>仄平<span class=\"ping\">平</span><span class=\"yun0\">（韵）</span>。</div></blockquote></div>");
            //cipai.getPingze();
            cipai.setPingze(pingze);
        }
        // 旧词编辑
        else if (writing != null) {
            cipai = writing.getCipai();
        } else {
            finish();
            Toast.makeText(this, "进入编辑出错", Toast.LENGTH_SHORT).show();
            return;
        }

        oldText = writing.getText();
        getBottomLeftView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                exit();
            }
        });
        setBottomGone();

        ImageView down = new TouchEffectImageView(mActivity, null);
        down.setImageResource(R.mipmap.done);
        down.setScaleType(ScaleType.FIT_XY);
        addBottomRightView(down, new LayoutParams(50, 50));
        down.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (currentIndex == 1) {
                    changeBackgroundFrament.save();
                } else if (currentIndex == 2) {
                    changePictureFragment.save();
                } else if (currentIndex == 0) {
                    editFragment.save();
                }
                save();
                writing.setBitmap(null);
//                Intent intent = new Intent(mActivity, ShareActivity.class);
//                intent.putExtra("writing", writing);
//                startActivity(intent);
                finish();
            }
        });

        initView();
    }

    private void initView() {

        final ImageView edit = new TouchEffectImageView(mActivity, null);
        edit.setScaleType(ScaleType.FIT_XY);
        edit.setImageResource(R.mipmap.layout_bg_edit_selected);

        final ImageView background = new TouchEffectImageView(mActivity, null);
        background.setScaleType(ScaleType.FIT_XY);
        background.setImageResource(R.mipmap.layout_bg_paper);

        final ImageView picture = new TouchEffectImageView(mActivity, null);
        picture.setScaleType(ScaleType.FIT_XY);
        picture.setImageResource(R.mipmap.layout_bg_album);

        edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                changeFragment(0);
                edit.setImageResource(R.mipmap.layout_bg_edit_selected);
                background.setImageResource(R.mipmap.layout_bg_paper);
                picture.setImageResource(R.mipmap.layout_bg_album);

            }
        });

        background.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                changeFragment(1);
                edit.setImageResource(R.mipmap.layout_bg_edit);
                background.setImageResource(R.mipmap.layout_bg_paper_selected);
                picture.setImageResource(R.mipmap.layout_bg_album);
            }
        });

        picture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                changeFragment(2);
                edit.setImageResource(R.mipmap.layout_bg_edit);
                background.setImageResource(R.mipmap.layout_bg_paper);
                picture.setImageResource(R.mipmap.layout_bg_album_sel);
            }
        });

        LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(DisplayUtil.dip2px(mActivity, 40),
                DisplayUtil.dip2px(mActivity, 45));
        lps.leftMargin = 20;
        addBottomCenterView(edit, lps);
        addBottomCenterView(background, lps);
        addBottomCenterView(picture, lps);

        // 创建修改实例
        editFragment = EditFragment.getInstance(cipai, writing);
        changeBackgroundFrament = ChangeBackgroundFragment.getInstance(cipai, writing);
        changePictureFragment = ChangePictureFragment.getInstance(cipai, writing);

        fragments.add(editFragment);
        fragments.add(changeBackgroundFrament);
        fragments.add(changePictureFragment);
        changeFragment(currentIndex);
    }

    public void changeFragment(int pos) {
        currentIndex = pos;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragments.get(pos));
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void exit() {
//        if (currentIndex == 1) {
//            changeBackgroundFrament.save();
//        } else if (currentIndex == 2) {
//            changePictureFragment.save();
//        }
//        editFragment.save();
//        if (! StringUtils.isEmpty(writing.getText()) && ! writing.getText().equals(oldText)) {
//            Bundle bundle = new Bundle();
//            bundle.putString(GCDialog.DATA_CONTENT, mActivity.getString(R.string.save_content));
//            bundle.putString(GCDialog.DATA_TITLE, mActivity.getString(R.string.save_title));
//            bundle.putString(GCDialog.CONFIRM_TEXT, mActivity.getString(R.string.save));
//            bundle.putString(GCDialog.CANCEL_TEXT, mActivity.getString(R.string.give_up));
//            new GCDialog(mActivity, new OnCustomDialogListener() {
//
//                @Override
//                public void onConfirm() {
//                    save();
//                    finish();
//                }
//
//                @Override
//                public void onCancel() {
//                    finish();
//                }
//            }, bundle).show();
//        } else {
//            finish();
//        }
    }

    private void save() {
        if (! StringUtils.isNumeric(writing.getBgimg()) && writing.getBitmap() != null) {
            try {
                String fileName = FileUtils.saveFile(writing.getBitmap());
                writing.setBgimg(fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        WritingDatabaseHelper.saveWriting(mActivity, writing);
    }

    @Override
    public void onBackPressed() {
        exit();
    }

}